package com.longka.dao;

import com.longka.bean.Book;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BooksInfoDao extends BaseDao {

    public RowMapper<Book> mapper = (rs, i) -> new Book(
            rs.getInt("id"),
            rs.getString("bookname"),
            rs.getString("author"),
            rs.getString("publishTime"),
            rs.getInt("grade")
    );


    public List<Book> queryAll() {
        String sql = "SELECT * FROM booksInfo";
        return jdbc.query(sql, mapper);
    }

    public int delete(int bookId) {
        return jdbc.update("DELETE FROM booksInfo WHERE id = ? ", bookId);
    }

    public int add(String bookName, String author, String time, int grade) {
        return jdbc.update("INSERT INTO booksInfo(bookname,author,publishTime,grade)VALUES (?,?,?,?)",
                bookName, author, time, grade);
    }
}
